﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace Abam.Code.OAuth
{
    public class GoogleUser
    {
        public string Access_Token;
        public string Token_Type;
        public string Display_name;
    }
    
    public class GoogleAuthorize
    {
        public static string ClientId
        {
            get { return "460110444551.apps.googleusercontent.com"; }
        }

        public static string ClientSecret
        {
            get { return "bps4Y5lZKee5UUB-jIo7SkCR"; }
        }

        public static string MyAuthorizeAddress
        {
            get { return "http%3A%2F%2Flocalhost:3951%2Fauth%2FGetGoogleToken"; }//todo сделать адрес в нормальном виде
        }

        public static string AuthorizeAddress
        {
            get
            {
                return "htttps://accounts.google.com/o/oauth2/auth?redirect_uri=" + MyAuthorizeAddress + 
                         "&response_type=code&client_id=" + ClientId +
                         "&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+" +
                         "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"; }
        }

        public static string GetTokenAddress
        {
            get { return "https://accounts.google.com/o/oauth2/token"; }
        }

        private static string GetInfoAddress
        {
            get { return "https://www.googleapis.com/oauth2/v1/userinfo?access_token="; }
        }

        public static WebResponse GetToken(string code)
        {
            var request = (HttpWebRequest)WebRequest.Create(GetTokenAddress);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            var query = String.Format("grant_type=authorization_code" +
                                      "&code={0}" +
                                      "&client_id={1}" +
                                      "&client_secret={2}" +
                                      "&redirect_uri={3}", code, ClientId, ClientSecret, MyAuthorizeAddress);

            var parameters = Encoding.UTF8.GetBytes(query);
            request.ContentLength = parameters.Length;

            var sendStream = request.GetRequestStream();
            sendStream.Write(parameters, 0, parameters.Length);
            sendStream.Close();

            var response = request.GetResponse();

            return response;
        }

        public static GoogleUser Deserialize(StreamReader streamReader)
        {
            var js = new JavaScriptSerializer();
            var user = js.Deserialize<GoogleUser>(streamReader.ReadToEnd());

            return user;
        }

        public static GoogleUser Info(string token)
        {
            var request = (HttpWebRequest) WebRequest.Create(GetInfoAddress + token);
            request.Method = "GET";

            var response = request.GetResponse();
            var sr = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
            var user = Deserialize(sr);
            sr.Close();

            return user;
        }
    }
}